import axios from 'axios'
// import store from '../store'
const Qs = require('qs')
import { Toast } from 'vant';
// 创建axios实例
const service = axios.create({
  timeout: 10000 // 请求超时时间
});
let requestData;
let loadingE;
// request拦截器
service.interceptors.request.use(
  request => {
    requestData=request
    if (request.isParams) {
      request.params = request.data
    }
    if(request.bfLoading){
      loadingE=Toast.loading({
        message: '加载中',
        forbidClick: true,
        loadingType: 'spinner'
      });
    }
    return request
  },
  error => {
    // request error
    Promise.reject(error)
  }
)
// response 拦截器
service.interceptors.response.use(
  response => {
    // code为非0是抛错
    if(loadingE) loadingE.clear();//停止页面loading
    const res = response.data;
    if (res.code == 0) {
      return response.data
    } else {
      Toast(res.code+"   "+res.msg);
      return Promise.reject("error")
    }
  },
  error => {
    if(loadingE) loadingE.clear();//停止页面loading
    Toast(error.message);
    return Promise.reject(error)
  }
);

export default function khReqMethod({ url,data,method,isParams,bfLoading,afHLoading,baseURL,timeout}) {
  return service({
    url: url,
    method: method || 'post',
    data: data||{},
    isParams:isParams||false,
    bfLoading: bfLoading || false,
    afHLoading: afHLoading || true,
    baseURL: baseURL || process.env.VUE_APP_BASE_URL, // 设置基本基础url
    timeout: timeout || 15000 // 配置默认超时时间
  })
}


